package stark.app.function.usersys;

import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.usersys.lib.UserModule;
import com.stark.usersys.lib.goods.bean.GoodsBean;
import com.stark.usersys.lib.user.bean.User;
import com.stark.usersyspay.lib.PayModule;

import org.jetbrains.annotations.NotNull;

import java.util.List;

import stark.app.R;
import stark.app.databinding.ActivityGoodsBinding;
import stark.common.basic.base.BaseNoModelActivity;
import stark.common.basic.retrofit.INewReqRetCallback;
import stark.common.basic.retrofit.IReqRetCallback;

/**
 * Author：daizhongbin on 2022/12/19
 * Email：1027154338@qq.com
 * Desc:
 */
public class GoodsActivity extends BaseNoModelActivity<ActivityGoodsBinding> {

    private GoodsAdapter mAdapter;
    private GoodsBean mSelGoods;

    @Override
    protected int onCreate() {
        return R.layout.activity_goods;
    }

    @Override
    protected void initView() {
        //rv
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        mDataBinding.rvGoods.setLayoutManager(layoutManager);
        mAdapter = new GoodsAdapter();
        mAdapter.setOnItemClickListener(this);
        mDataBinding.rvGoods.setAdapter(mAdapter);

        //btn pay
        mDataBinding.btnAliPay.setOnClickListener(this);
        mDataBinding.btnWxPay.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        UserModule.goodsApi().getGoodsList(this, new INewReqRetCallback<List<GoodsBean>>() {
            @Override
            public void onResult(int code, String msg, @Nullable List<GoodsBean> result) {
                if (mAdapter != null) {
                    mAdapter.setNewInstance(result);
                }
            }
        });
    }

    @Override
    protected void onItemClickCallback(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter, @NonNull @NotNull View view, int position) {
        mSelGoods = mAdapter.getItem(position);
        mDataBinding.tvSelGoods.setText("所选商品：" + mAdapter.getItem(position).toString());
    }

    @Override
    protected void onClickCallback(View v) {
        if (v == mDataBinding.btnWxPay) {
            clickPay(false);
        } else if (v == mDataBinding.btnAliPay) {
            clickPay(true);
        }
    }

    private void clickPay(boolean isAliPay) {
        if (mSelGoods == null) {
            ToastUtils.showShort("请先选择商品");
            return;
        }
        showDialog("支付中...");
        IReqRetCallback<Boolean> callback = new IReqRetCallback<Boolean>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable Boolean result) {
                dismissDialog();
                ToastUtils.showShort(msg);
                if (success) {
                    mDataBinding.btnWxPay.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            updateUser();
                        }
                    }, 3000);
                }
            }
        };
        if (isAliPay) {
            PayModule.purchaser().purchaseByAlipay(this, mSelGoods, callback);
        } else {
            PayModule.purchaser().purchaseByWechat(this, mSelGoods, callback);
        }
    }

    private void updateUser() {
        showDialog("刷新用户数据中...");
        UserModule.userApi().getUserInfo(GoodsActivity.this, new INewReqRetCallback<User>() {
            @Override
            public void onResult(int code, String msg, @Nullable User result) {
                dismissDialog();
                if (result != null) {
                    Log.e("dzb", "getUserInfo: user = " + result.toString());
                    ToastUtils.showLong(result.toString());
                }
            }
        });
    }
}
